package com.ruoyi.erecord.iot.handle;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.erecord.iot.config.Cfg;
import com.ruoyi.erecord.iot.mapper.CommonMapper;
import com.ruoyi.erecord.iot.service.CommonService;
import com.ruoyi.erecord.iot.tools.IDGen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import topevery.syn.data.model.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Service
public class IotDataHandle implements IIotDataSubscribeService {

    @Resource
    CommonMapper mapper;

    @Resource
    CommonService service;

    // private CommonMapper commonMapper ;


    /***验证账号密码*/
    @Override
    public boolean checkHeader(String uid, String pwd) {
        return Cfg.uid.equalsIgnoreCase(uid) && Cfg.pwd.equalsIgnoreCase(pwd);
    }

    /**
     * 业务端提供设备列表，供IOT平台调用
     **/
    @Override
    public List<EquipmentInfo> getEquipmentInfos(String labelCode) {
        List<EquipmentInfo> result = new ArrayList<>();
        Set<String> codes = mapper.getDeviceCode();
        codes.forEach(c -> {
            EquipmentInfo equipmentInfo = new EquipmentInfo();
            equipmentInfo.setDeviceCode(c);
            equipmentInfo.setDataType(ModelTypeEnum.Gps.getValue());
            equipmentInfo.setDeviceName("设备-" + c);
            equipmentInfo.setProductCode("");
            result.add(equipmentInfo);
        });
        return result;
    }

    /**
     * 保存设备数据，需要实现
     **/
    @Override
    public void setEquipmentDatas(String labelCode, List<EquipmentData> datas) {
        if (datas.size() < 1) return;

        List<GpsData> gpsDataList = new ArrayList<>();
        List<StateData> stateDataList = new ArrayList<>();
        datas.forEach(e -> {
            gpsDataList.addAll(e.listGpsData);
            stateDataList.addAll(e.listStateData);
        });
        int saveSize = gpsDataList.size();
        int stateSize = stateDataList.size();
        log.info("process start ......> GSPData Size > {}; stateSize > {}", saveSize, stateSize);

        if (saveSize > 0) {
            gpsDataList.stream().filter(bean -> {
                bean.setGid(IDGen.longTimeId());
                return true;
            }).collect(Collectors.toList());
            mapper.gpsDatasInsert(gpsDataList);
            service.gpsDataFill();
            service.gpsDataRepeatDel();
        }
        if (stateSize > 0) {
            stateDataList.stream().filter(bean -> {
                bean.setGid(IDGen.longTimeId());
                return true;
            }).collect(Collectors.toList());
            mapper.stateDatasInsert(stateDataList);
            service.stateDataFill();
        }
        log.info("process end ------>");

    }


    /**
     * 返回设备列表，需要实现
     **/
    @Override
    public List<DataForwarding> getDataForwarding(String labelCode) {

        log.info("??????????? call getDataForwarding");
        return null;
        // FileReader file = new FileReader("/data/jar-package/topevery-syn-client-wapi/device-data-forwarding.txt");
        // List<String> strList = file.readLines();
        // List<DataForwarding> result = new ArrayList<>();
        // for (String str : strList) {
        //     String[] splitStr = str.split("#");
        //     DataForwarding equipmentInfo = new DataForwarding();
        //     equipmentInfo.setDeviceCode(splitStr[0]);
        //     equipmentInfo.setIpAddress(splitStr[1]);
        //     equipmentInfo.setPort(Integer.parseInt(splitStr[2]));
        //     result.add(equipmentInfo);
        // }
        // return result;
    }


    /**
     * 保存告警数据，需要实现
     **/
    @Override
    public void setAlarmData(String labelCode, AlarmInfoData data) {
        log.info("labelCode:{},data:{}", labelCode, JSONObject.toJSON(data));
        System.out.println(data);
    }

    /* 获得16个长度的十六进制的UUID

     * @return UUID

     */
    public static String get16UUID() {

        UUID id = UUID.randomUUID();

        String[] idd = id.toString().split("-");

        return idd[0] + idd[1] + idd[2];

    }
}
